--- Provides a wrapper for a 'Monoid' that works in reverse
module Data.wrapper.Dual where

import Data.Monoid

--- Provides a 'Monoid' where 'mappend' appears flipped.
newtype Dual a = Dual { unwrap :: a } --- wrap a value to give a 'Dual'
derive Show (Dual a)
derive Eq (Dual a)
derive Ord (Dual a)

--- get the value wrapped by 'Dual' (Haskell compatibility)
getDual = Dual.unwrap

instance Semigroup a => Semigroup  (Dual a) where
    --- > Dual "foo" <> Dual "bar" == Dual "barfoo"
    Dual x `mappend` Dual y = Dual (mappend y x)

instance Monoid a => Monoid (Dual a) where
    --- @Dual e@ where @e@ is the identity of the wrapped value.
    mempty = Dual mempty

